﻿using System;
using System.Collections;
using System.Text;
using System.IO;

namespace F1_Start_Sequence
{
    class TrackRaceLocation
    {
        public Hashtable TRL = new Hashtable();
        public string storetrackname = "";

        /// <summary>
        /// Reads the handicaps from txt file
        /// </summary>
        /// <param name="filename">The filename.</param>
        public void ReadTrackLocations(string trackname)
        {
            try
            {
                if (trackname != storetrackname)
                {
                    TRL.Clear();
                    StreamReader re = File.OpenText(trackname + ".txt");
                    string input = null;
                    int position = 0;
                    string splitString = "";
                    char[] sep = { ',' };

                    while ((input = re.ReadLine()) != null)
                    {
                        TrackRaceLocation trl = new TrackRaceLocation();
                        splitString = input;
                        Array a = splitString.Split(sep);
                        for (int i = 0; i < a.Length; i++)
                        {
                            Console.WriteLine(a.GetValue(i).ToString());
                            if (i == 0)
                            {
                                //position = (int)a.GetValue(i);
                                position = Convert.ToInt16(a.GetValue(i));
                                trl.position = Convert.ToInt16(a.GetValue(i));
                            }
                            if (i == 1)
                            {
                                trl.X = Convert.ToInt32(a.GetValue(i));
                            }
                            if (i == 2)
                            {
                                trl.Y = Convert.ToInt32(a.GetValue(i));
                            }
                        }
                        TRL.Add(position, trl);
                    }
                    re.Close();
                }
                storetrackname = trackname;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
                Console.ReadLine();
                Environment.Exit(0);
            }
        }

        public void WriteTrackLocations(string trackname)
        {
            try
            {
                StreamWriter wr = new StreamWriter(trackname + ".txt");

                foreach (DictionaryEntry entry in TRL)
                {
                    //TrackRaceLocation trl = new TrackRaceLocation();
                    TrackRaceLocation trl = (TrackRaceLocation)entry.Value;
                    wr.WriteLine(trl.position + "," + trl.X + "," + trl.Y);
                }
                wr.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
            }
        }

        private int _position;

        public int position
        {
            get { return _position; }
            set { _position = value; }
        }

        private int _X;

        public int X
        {
            get { return _X; }
            set { _X = value; }
        }

        private int _Y;

        public int Y
        {
            get { return _Y; }
            set { _Y = value; }
        }
    }
}
